import api from './api';

// API响应格式
interface ApiResponse<T> {
  data: T;
  message: string;
  status: string;
}

// 教师接口
export interface Teacher {
  id: number;
  username: string;
  password?: string;
  name: string;
  phone: string | null;
  email: string | null;
  avatar: string | null;
  role: string;
  enabled: boolean;
  createTime: string;
  updateTime: string;
}

// 教师列表项类型
export interface TeacherListItem {
  id: number;
  name: string;
}

// 分页响应接口
export interface PageResponse<T> {
  content: T[];
  pageable: {
    pageNumber: number;
    pageSize: number;
    sort: {
      empty: boolean;
      sorted: boolean;
      unsorted: boolean;
    };
    offset: number;
    paged: boolean;
    unpaged: boolean;
  };
  last: boolean;
  totalElements: number;
  totalPages: number;
  size: number;
  number: number;
  sort: {
    empty: boolean;
    sorted: boolean;
    unsorted: boolean;
  };
  numberOfElements: number;
  first: boolean;
  empty: boolean;
}

// 查询参数接口
export interface TeacherQueryParams {
  page?: number;
  size?: number;
  keyword?: string;
}

// 创建教师参数接口
export interface CreateTeacherParams {
  username: string;
  password: string;
  name: string;
  phone: string;
  email: string;
  role: string;
  enabled: boolean;
}

// 更新教师参数接口
export interface UpdateTeacherParams {
  name?: string;
  phone?: string;
  email?: string;
}

// 获取教师列表
export const getTeachers = async (params: TeacherQueryParams = {}): Promise<PageResponse<Teacher>> => {
  const { page = 0, size = 10, keyword = '' } = params;
  const  response  = await api.get<PageResponse<Teacher>>('/admin/teachers', {
    params: { page, size, keyword }
  });
  console.log('API原始响应:', response);
  //response就正确返回了不要修改这里
  return response;
};

/**
 * 获取简易教师列表
 * @returns 教师列表
 */
export const getTeacherList = async (): Promise<TeacherListItem[]> => {
  try {
    const { data } = await api.get('/admin/teachers/list');
    console.log('获取教师列表响应:', data);
    
    // 检查响应数据结构
    if (data && data.data && Array.isArray(data.data)) {
      console.log('获取教师列表数据:', data.data);
      return data.data;
    } else if (data && Array.isArray(data)) {
      // 如果直接返回数组
      console.log('获取教师列表数据(直接数组):', data);
      return data;
    } else {
      console.warn('教师列表数据格式不符合预期:', data);
      return [];
    }
  } catch (error) {
    console.error('获取教师列表失败:', error);
    return [];
  }
};

// 获取教师详情
export const getTeacher = async (id: number): Promise<Teacher> => {
  const response = await api.get<Teacher>(`/admin/teachers/${id}`);
  return response.data;
};

// 创建教师
export const createTeacher = async (params: CreateTeacherParams): Promise<Teacher> => {
  const response = await api.post<Teacher>('/admin/teachers', {
    ...params,
    role: 'TEACHER',
    enabled: true
  });
  return response.data;
};

// 更新教师
export const updateTeacher = async (id: number, params: UpdateTeacherParams): Promise<Teacher> => {
  const response = await api.put<Teacher>(`/admin/teachers/${id}`, params);
  return response.data;
};

// 删除教师
export const deleteTeacher = async (id: number): Promise<void> => {
  await api.delete(`/admin/teachers/${id}`);
};

// 更新教师状态
export const updateTeacherStatus = async (id: number, enabled: boolean): Promise<Teacher> => {
  const response = await api.put<Teacher>(`/admin/teachers/${id}/status`, { enabled });
  return response.data;
}; 